#define  _CRT_SECURE_NO_WARNINGS

#include "stack.h"

void StackInit(Stack* st)
{
	assert(st);
	st->arr = NULL;
	st->top = st->capacity = 0;
}

void StackPush(Stack* st, SVType x)
{
	assert(st);
	if (st->top == st->capacity)
	{
		int newcapacity = st->capacity == 0 ? 4 : 2 * sizeof(SVType);
		SVType* tmp = (SVType*)realloc(st->arr, newcapacity * sizeof(SVType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		st->arr = tmp;
		tmp = NULL;
		st->capacity = newcapacity;
	}
	st->arr[st->top] = x;
	st->top++;
}

void StackPop(Stack* st)
{
	assert(st);
	assert(st->top > 0);
	st->top--;
}

SVType StackTop(Stack* st)
{
	assert(st);
	assert(st->top > 0);
	return st->arr[st->top - 1];
}

bool StackEmpty(Stack* st)
{
	assert(st);
	return st->top == 0;
}

void StackDestroy(Stack* st)
{
	assert(st);
	free(st->arr);
	st->arr = NULL;
	st->top = st->capacity = 0;
}